//
//  NDContactsSection.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/31.
//

import SwiftUI

struct NDContactsSection: View {
    var viewModel: NDContactsViewModel

    let pachist: Pachist
    let commonActionSectionIndex: Int
    
    let onClickEnumButton: () -> Void
    let onClickContactsButton: () -> Void
    
    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            let title = pachist.noable ?? ""
            Text(title)
                .frame(maxWidth: .infinity, alignment: .center)
                .font(.system(size: 14, weight: .medium))
                .foregroundColor(Color(hex: "#271F24"))
                .padding(.top, commonActionSectionIndex == 0 ? 16 : 0)
            
            ForEach(0...1, id: \.self) { index in
                itemView(for: index)
                    .id(index)
            }
            .padding(.bottom, commonActionSectionIndex == 0 ? 0 : 16)
        }
        .padding(.horizontal, 16)
    }
    
    @ViewBuilder
    private func itemView(for index: Int) -> some View {
        if index == 0 {
            let binding = viewModel.createPachistBinding(at: commonActionSectionIndex, field: .olig)
            
            // 第一个项目的视图
            NDCommonActionItem(
                title: pachist.himselfful ?? "",
                textContent: binding,
                actionType: .button,
                plactHolder: pachist.appearoon ?? "",
                keboardType: .default,
                fieldId: 0,
                onBeginEditing: { fieldId in
      
                },
                onEndEditing: {
  
                },
                onClickButton: {
                    viewModel.commonActionSectionIndex = commonActionSectionIndex
                    onClickEnumButton()
                }
            )
        } else {
            
            let binding = viewModel.createPachistBinding(at: commonActionSectionIndex, field: .pressoutside)
            
            NDCommonActionItem(
                title: pachist.productionence ?? "",
                textContent: binding,
                actionType: .button,
                plactHolder: pachist.wouldator ?? "",
                keboardType: .default,
                fieldId: 1,
                onBeginEditing: { fieldId in

                },
                onEndEditing: {
   
                },
                onClickButton: {
                    viewModel.commonActionSectionIndex = commonActionSectionIndex
                    onClickContactsButton()
                }
            )
        }
    }
}
